package com.litchi.tutor.common.base;

import lombok.Data;

/**
 * 通用返回类
 **/
@Data
public class ResponseVO<T> {

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;

    public ResponseVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseVO<T> ok(T data) {
        ResponseVO<T> responseVO = new ResponseVO<>(200, "success");
        responseVO.setData(data);
        return responseVO;
    }

    public static <T> ResponseVO<T> fail(String msg) {
        ResponseVO<T> responseVO = new ResponseVO<>(400, "client request fail");
        if (msg != null && !msg.isEmpty()) {
            responseVO.setMsg(msg);
        }
        return responseVO;
    }

    public static <T> ResponseVO<T> error(String msg) {
        ResponseVO<T> responseVO = new ResponseVO<>(500, "internal server error");
        if (msg != null && !msg.isEmpty()) {
            responseVO.setMsg(msg);
        }
        return responseVO;
    }
}
